﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-13 22:14:57
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
using OF.Component.DTO;

namespace OF.Component.DAL
{

    /// <summary>
    ///收件地址 数据访问层
    /// </summary>	
    internal partial class MailAddress
    {

        /// <summary>
        /// 根据断言条件获取单个收件地址
        /// </summary>
        public MailAddressInfo QuerySingle(Expression<Func<MailAddressInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 根据断言条件获获取收件地址列表
        /// </summary>
        public IList<MailAddressInfo> QueryList(Expression<Func<MailAddressInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 获取默认收件地址
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public MailAddressInfo GetDefault(long userId)
        {
            string sql = "SELECT * FROM MailAddress WHERE IsActive=1 AND UserID=@UserID ORDER BY IsDefault DESC";
            Model.MailAddressInfo info = null;
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sql);
            db.AddInParameter(dbc, "@UserID", DbType.Int64, userId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

        /// <summary>
        /// 根号用户查询
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="total"></param>
        /// <returns></returns>
	    public IList<MailAddressDto> SearchList(MailAddressConditon condition, out int total)
        {
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("up_MailAddressSearchList");

            db.AddInParameter(dbc, "@UserID", DbType.Int64, condition.UserID);
            db.AddInParameter(dbc, "@PageIndex", DbType.Int32, condition.page);
            db.AddInParameter(dbc, "@PageSize", DbType.Int32, condition.rows);
            db.AddOutParameter(dbc, "@OutCount", DbType.Int32, 4);

            DataSet dr = db.ExecuteDataSet(dbc);
            total = (int)db.GetParameterValue(dbc, "@OutCount");

            return SQLServerProvider.CreateList<MailAddressDto>(dr.Tables[0]);
        }
    }

}